import React from 'react';
import styles from './index.less';
import _ from 'lodash';
import { countNumber, NumberFormat2 } from '@/utils';

export const powerSituationFloat = {
  testLoss: {
    title: '考核损失电量',
    children: {
      transUnPlan: '输变电非计划',
      windUnPlan: '风机非计划',
      transPlan: '输变电计划',
      windPlan: '风机计划',
    },
  },
  otherLoss: {
    title: '其他损失电量',
    children: {
      disasterLoss: '自然灾害',
      faultLoss: '电网故障',
    },
  },
};

/*风电可利用率 故障复现率*/
export const utilizationAndFaultRateTitle = {
  windfarmUseRateBodyList: '场站可利用率',
  faultRateBodyList: '故障复现率',
};
/*
  isTag true在数据后面加标签
  tag true整个框只有标签
  percentage是否是百分比
  tagValue标签值单独判断
  tagPercentage true%存在乘100
  noToolTip  不展示提示气泡框  
*/

export const operateColumnsData = {
  name: { title: '中心', width: 60, ellipsis: false, noToolTip: true },
  windfarmUseRate: {
    title: '风电场可利用率',
    width: 106,
    percentage: true,
    ellipsis: false,
    precision: 2,
    noToolTip: true,
  },
  tongqi: { title: '同期', percentage: true, width: 106, precision: 2, noToolTip: true },
  tongbi: {
    title: '同比',
    tag: true,
    tagPercentage: true,
    ellipsis: false,
    width: 106,
    precision: 2,
    noToolTip: true,
  },
  paln: { title: '计划值', percentage: true, width: 106, precision: 2, noToolTip: true },
  palnDeviation: {
    title: '计划值偏差',
    width: 106,
    percentage: true,
    precision: 2,
    noToolTip: true,
  },

  theoretical: {
    title: '理论电量',
    align: 'center',
    children: ['llDl', 'llDlFloat', 'llDlHour', 'llDlHourFloat'],
  },
  llDl: { title: '理论电量', width: 110 },
  llDlFloat: { title: '同比', width: 110, tag: true },
  llDlHour: { title: '等效理论小时数', width: 120 },
  llDlHourFloat: { title: '同比', width: 110, tag: true },

  terminal: {
    title: '机端电量',
    align: 'center',
    children: ['jdDl', 'jdDlFloat', 'jdDlHour', 'jdDlHourFloat'],
  },
  jdDl: { title: '机端电量', width: 110 },
  jdDlFloat: { title: '同比', width: 110, tag: true },
  jdDlHour: { title: '等效机端小时数', width: 120 },
  jdDlHourFloat: { title: '同比', width: 110, tag: true },

  limit: {
    title: '限电电量',
    align: 'center',
    children: [
      'limitQuantity',
      'limitFloat',
      'limitRat',
      'limitRatFlag',
      'limitPlanRat',
      'limitDiff',
    ],
  },
  limitQuantity: { title: '限电电量', width: 110 },
  limitFloat: { title: '同比', width: 110, tag: true },
  limitRat: { title: '限电率', width: 110, percentage: true, precision: 2 },
  limitRatFlag: {
    title: '同比',
    width: 110,
    precision: 2,
    percentage: true,
    tagPercentage: true,
    tag: true,
  },
  limitPlanRat: { title: '计划限电率', width: 110, percentage: true, precision: 2 },
  limitDiff: { title: '限电偏差', width: 110 },

  loss: {
    title: '损失电量',
    align: 'center',
    children: ['lossQuantity', 'ssDlFloat', 'lossHour', 'lossFloat'],
  },
  lossQuantity: { title: '损失电量', width: 110, placement: 'left', floatObj: 'lossQuantityFloat' },
  ssDlFloat: { title: '同比', width: 110, tag: true },
  lossHour: { title: '等效损失小时数', width: 120 },
  lossFloat: { title: '同比', width: 110, tag: true },

  online: {
    title: '上网电量',
    align: 'center',
    children: [
      'onlineQuantity',
      'onlineFlag',
      'onlinePlanQuantity',
      'onlineRate',
      'finishHour',
      'finishFlag',
    ],
  },
  onlineQuantity: {
    title: '上网电量',
    showSorterTooltip: false,
    width: 110,
    precision: 3,
    sorter: (a, b) => a.onlineQuantity - b.onlineQuantity,
  },
  onlineFlag: { title: '同比', width: 110, tag: true },
  onlinePlanQuantity: { title: '计划上网电量', width: 120 },
  onlineRate: { title: '上网电量完成率', width: 120, percentage: true, precision: 2 },
  finishHour: { title: '等效完成小时数', align: 'left', width: 120 }, //finishHour
  finishFlag: { title: '同比', align: 'left', width: 110, tag: true },

  factory: {
    title: '场用',
    sanKeyName: '综合场用',
    align: 'center',
    children: ['cyDl', 'cyDlFloat', 'factoryRat', 'factoryFlag', 'cyHour', 'cyHourFloat'],
  },
  cyDl: { title: '场用电量', width: 110 },
  cyDlFloat: { title: '同比', width: 110, tag: true },
  factoryRat: { title: '场用电率', width: 100, percentage: true, precision: 2 },
  factoryFlag: { title: '同比', width: 110, tag: true },
  cyHour: { title: '等效场用小时数', width: 120 },
  cyHourFloat: { title: '同比', width: 110, tag: true },

  fjPlan: {
    title: '风机计划',
    align: 'center',
    children: ['fjJhSsDl', 'fjJhSsFloat', 'fjJhSsHour', 'fjJhSsHourFloat'],
  },
  fjJhSsDl: { title: '风机计划损失电量', width: 120, placement: 'left' },
  fjJhSsFloat: { title: '同比', width: 110, tag: true },
  fjJhSsHour: { title: '等效风机计划损失小时数', width: 130 },
  fjJhSsHourFloat: { title: '同比', width: 110, tag: true },

  fjUnPlan: {
    title: '风机非计划',
    align: 'center',
    children: ['fjFjhSsDl', 'fjFjhSsFloat', 'fjFjhSsHour', 'fjFjhSsHourFloat'],
  },
  fjFjhSsDl: { title: '风机非计划损失电量', width: 120, placement: 'left' },
  fjFjhSsFloat: { title: '同比', width: 110, tag: true },
  fjFjhSsHour: { title: '等效风机非计划损失小时数', width: 130 },
  fjFjhSsHourFloat: { title: '同比', width: 110, tag: true },

  sbdPlan: {
    title: '输变电计划',
    align: 'center',
    children: ['sbdJhSsDl', 'sbdJhSsFloat', 'sbdJhSsHour', 'sbdJhSsHourFloat'],
  },
  sbdJhSsDl: { title: '输变电计划损失电量', width: 120, placement: 'left' },
  sbdJhSsFloat: { title: '同比', width: 110, tag: true },
  sbdJhSsHour: { title: '等效输变电计划损失小时数', width: 130 },
  sbdJhSsHourFloat: { title: '同比', width: 110, tag: true },

  sbdUnPlan: {
    title: '输变电非计划',
    align: 'center',
    children: ['sbdFjhSsDl', 'sbdFjhSsFloat', 'sbdFjhSsHour', 'sbdFjhSsHourFloat'],
  },
  sbdFjhSsDl: { title: '输变电非计划损失电量', width: 120, placement: 'left' },
  sbdFjhSsFloat: { title: '同比', width: 110, tag: true },
  sbdFjhSsHour: { title: '等效输变电非计划损失小时数', width: 130 },
  sbdFjhSsHourFloat: { title: '同比', width: 110, tag: true },

  failure: {
    title: '电网故障',
    align: 'center',
    children: ['dwGzSsDl', 'dwGzSsFloat', 'dwGzSsHour', 'dwGzSsHourFloat'],
  },
  dwGzSsDl: { title: '电网故障损失电量', width: 120, placement: 'left' },
  dwGzSsFloat: { title: '同比', width: 110, tag: true },
  dwGzSsHour: { title: '等效电网故障损失小时数', width: 130 },
  dwGzSsHourFloat: { title: '同比', width: 110, tag: true },

  natural: {
    title: '自然灾害',
    align: 'center',
    children: ['zrZhSsDl', 'zrZhSsFloat', 'zrZhSsHour', 'zrZhSsHourFloat'],
  },
  zrZhSsDl: { title: '自然灾害损失电量', width: 120, placement: 'left' },
  zrZhSsFloat: { title: '同比', width: 110, tag: true },
  zrZhSsHour: { title: '等效自然灾害损失小时数', width: 120 },
  zrZhSsHourFloat: { title: '同比', width: 110, tag: true },
};

/**
 * 通过桑基图获得表格column
 * @method bySanKeyGetColumn
 * @param  {Array} arr columns数据的key
 * @return {Array} Table的columns数组
 */
export const bySanKeyGetColumn = (arr) => {
  return arr.map((item) => {
    let key = '';
    let flag = true;
    item = item.trim() === '计划上网电量' ? '上网电量' : item;
    Object.keys(operateColumnsData).forEach((n) => {
      let title = item.trim();
      if (
        (title === operateColumnsData[n].title || title === operateColumnsData[n].sanKeyName) &&
        flag
      ) {
        key = n;
        flag = false;
      }
    });
    return key;
  });
};

/**
 * 发电情况表格Columns
 * @method operateColumns
 * @param  {Array} columnsData columns数据的key
 * @param  {Function} tableCol render函数
 * @param {String} type 单独判断的type
 * @return {Array} Table的columns
 */
export const operateColumns = (columnsData, tableCol, type) => {
  let columns = columnsData.map((key) => {
    let item = { ellipsis: true, ...operateColumnsData[key] };
    item.key = key + '';
    item.dataIndex = key + '';
    item.render = (text, data) => tableCol(text, data, item);
    if (item.children && item.children.length) {
      item.children = operateColumns(item.children, tableCol);
    }
    if (key === 'windfarmUseRate') {
      item.title = utilizationAndFaultRateTitle[type];
    }
    if (type === 'powerSituation' && key === 'name') {
      item.fixed = true;
      item.width = 150;
      item.key = 'powerSituationName';
      item.ellipsis = true;
      item.title = <div style={{ marginLeft: 25 }}>中心</div>;
    }
    return item;
  });
  return columns;
};

/**
 * 获取桑基图option
 * @method getSanKeyOption
 * @param  {Array} nodes nodes
 * @param  {Array} links links
 * @return {Object} 桑基图option
 */
export const getSanKeyOption = function (nodes, links) {
  let series = []; //图表显示数据的集合
  let extendArr = [];
  let tooltip = {
    trigger: 'item',
    triggerOn: 'mousemove',
    extraCssText: 'box-shadow: 0 0 10px rgba(0, 0, 0, 0.1)',
    backgroundColor: 'rgba(255, 255, 255, 1)',
    textStyle: { color: '#888E95', borderWidth: 1, borderColor: '#1A253C4B', padding: 15 },
  };
  tooltip.axisPointer = {
    type: 'cross',
    label: { backgroundColor: '#F5F8FA', color: '#888E95', shadowBlur: 0 },
  };
  tooltip.formatter = (params) => {
    let arr = params.name.split('>');
    let name =
      arr.length > 1
        ? arr[0].split('(')[0].trim() + '-' + arr[1].split('(')[0].trim()
        : arr[0].split('(')[0].trim();
    return `<div>${name}：<span style="color: #373E48;">${params.value}万kWh</span></div>`;
  };
  let serie = { type: 'sankey', layout: 'none', focusNodeAdjacency: 'allEdges' };
  serie.left = 0;
  serie.right = '19%';
  serie.draggable = false;
  let nodeArray = _.uniqBy(nodes, 'name').map((item) => {
    if (item.typeEditing) {
      extendArr.push(item.name);
    }
    let tvalue = Number(item.quantity) || 0;
    return {
      label: {
        color: '#373E48',
        show: true,
        formatter: (n) => {
          return n.name + item.detail;
        },
      },
      depth: item.depth || 0,
      name: item.name,
      xvalue: tvalue,
      value: NumberFormat2(Math.abs(tvalue), 3),
      itemStyle: {
        color: tvalue < 0 ? '#ED5945' : item.color,
        borderWidth: item.borderColor ? 1 : 1,
        borderColor: item.borderColor || item.color,
      },
    };
  });
  serie.data = nodeArray;
  serie.links = links.map((item) => {
    return {
      source: item.source,
      target: item.target,
      value: NumberFormat2(Math.abs(item.quantity), 3),
      xvalue: Number(item.quantity),
    };
  });
  serie.lineStyle = { color: 'source', curveness: 0.3 };
  serie.nodeGap = 20;
  serie.layoutIterations = 0;
  serie.focusNodeAdjacency = false;
  series.push(serie);
  return {
    grid: { left: 0, top: 20, right: '30%', height: '100%', containLabel: true },
    toolbox: { show: false },
    tooltip: tooltip,
    series: series,
  };
};

export let setArray = (arr1, arr2) => {
  //数组拼接 去重
  let array = [];
  let concatArr = array.concat(arr1, arr2);
  let newarr = [];
  let newobj = {};
  for (let i = 0; i < concatArr.length; i++) {
    if (!newobj[concatArr[i] && concatArr[i].name]) {
      newarr.push(concatArr[i]);
      newobj[concatArr[i] && concatArr[i].name] = true;
    }
  }
  return newarr;
};

export let flatArr = (arr) => {
  //扁平化数组
  let array = [];
  arr.forEach((item) => {
    if (item.children && item.children.length > 0) {
      array = [...array, ...flatArr(item.children)];
    } else {
      array.push(item);
    }
  });
  return array;
};

/**
 * 处理发电情况表格
 * @method handlePowerSituationNodes
 * @param  {Array} data 处理数据
 * @return {Array}
 */
export const handlePowerSituationNodes = (data) => {
  return data.map((item) => {
    item = { ...item, ...item.col };
    if (item.childrenList && item.childrenList.length) {
      item.children = handlePowerSituationNodes(item.childrenList) || [];
    }
    return item;
  });
};

/**
 * 处理发电情况表格浮动Tooltip
 * @method handleFloatTooltip
 * @param {Array} data 处理数据
 * @param {Object} columnsItem 当前行
 * @return {String}
 */
export const handleFloatTooltip = (data, columnsItem) => {
  let percentage = columnsItem.percentage ? '%' : '';
  return (
    <div className={styles.powerSituationFloatBox}>
      <div className={styles.rowName}>{data.name}</div>
      {Object.keys(powerSituationFloat).map((n, index) => {
        return (
          <div className={styles.content} key={index}>
            <div className={styles.item}>
              <span className={styles.itemLeft}>{powerSituationFloat[n].title}</span>
              <span className={styles.itemRight}>
                {columnsItem.percentage
                  ? countNumber(data[columnsItem.floatObj][n], 100, '*')
                  : data[columnsItem.floatObj][n]}
                {percentage}
              </span>
            </div>
            <div className={styles.bottom}>
              <div className={styles.bottomLeft} />
              <div className={styles.bottomRight}>
                {Object.keys(powerSituationFloat[n].children).map((j, i) => {
                  return (
                    <div className={styles.item} key={i}>
                      <span className={styles.itemLeft}>{powerSituationFloat[n].children[j]}</span>
                      <span className={styles.itemRight}>
                        {columnsItem.percentage
                          ? countNumber(data[columnsItem.floatObj][j], 100, '*')
                          : data[columnsItem.floatObj][j]}
                        {percentage}
                      </span>
                    </div>
                  );
                })}
              </div>
            </div>
          </div>
        );
      })}
    </div>
  );
};
